<template>
  <div class="app-container">
    <el-form
      :inline="true"
      @keyup.enter.native="getDataList()"
      size="small"
      v-show="showSearch"
    >
      <el-form-item label="随访标题">
        <el-input
          v-model="queryParams.visitTitle"
          style="width: 230px"
          placeholder="请输入随访标题"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="患者编号">
        <el-input
          v-model="queryParams.patientId"
          style="width: 230px"
          placeholder="请输入患者编号"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="患者来源">
        <el-cascader
          style="width: 230px"
          :props="defaultProps"
          @change="handleSubChange"
          clearable
          :options="deptOptions"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="随访状态">
        <el-select
          clearable
          style="width: 230px"
          v-model="queryParams.visitStatus"
          placeholder="请选择"
        >
          <el-option
            v-for="item in dict.type.gonitis_visit_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 230px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getDataList"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-setting"
          size="mini"
          @click="toVisitForm"
          v-hasPermi="['gonitis:visit:module:list']"
          >随访模块管理</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle(0)"
          v-hasPermi="['gonitis:visit:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="updateHandle()"
          v-hasPermi="['gonitis:visit:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="deleteHandle(0)"
          v-hasPermi="['gonitis:visit:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getDataList"
      ></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" align="center" width="55"></el-table-column>
      <el-table-column prop="id" align="center" label="随访编号"></el-table-column>
      <el-table-column
        prop="patientId"
        align="center"
        label="患者编号"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="patientName"
        align="center"
        label="患者姓名"
      ></el-table-column>
      <el-table-column prop="doctorId" align="center" label="医生编号"></el-table-column>
      <el-table-column prop="doctorName" align="center" label="医生姓名">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName ? scope.row.doctorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="subTitle"
        align="center"
        label="患者来源"
        :show-overflow-tooltip="true"
        >膝关节炎</el-table-column
      >
      <el-table-column
        prop="visitTitle"
        align="center"
        label="随访标题"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        width="100"
        label="创建时间"
      ></el-table-column>
      <el-table-column prop="visitCount" align="center" label="随访轮次">
        <template slot-scope="scope" v-if="scope.row.visitCount">
          <span>第 {{ scope.row.visitCount }} 次</span>
        </template>
      </el-table-column>
      <el-table-column prop="visitStatus" align="center" label="随访状态">
        <template slot-scope="scope">
          <el-popover
            :disabled="!(scope.row.visitStatus === 2)"
            placement="top-start"
            width="150px"
            trigger="hover"
          >
            <el-button
              style="width: 150px"
              @click="completeVisit(scope.row.id)"
              size="small"
              type="primary"
              plain
            >
              去完善
              <i class="el-icon-d-arrow-right"></i>
            </el-button>
            <dict-tag
              slot="reference"
              :options="dict.type.gonitis_visit_status"
              :value="scope.row.visitStatus"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        class-name="small-padding fixed-width"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(scope.row.id)"
            v-hasPermi="['gonitis:visit:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row.id)"
            v-hasPermi="['gonitis:visit:remove']"
            >删除</el-button
          >
          <el-dropdown
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
            v-hasPermi="['gonitis:visit:detail', 'gonitis:visit:edit']"
          >
            <el-button type="text" size="mini" icon="el-icon-d-arrow-right"
              >更多</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="detailHandle"
                icon="el-icon-view"
                v-hasPermi="['gonitis:visit:detail']"
                >查看随访详情</el-dropdown-item
              >
              <el-dropdown-item
                command="editStepHandle"
                icon="el-icon-edit"
                v-hasPermi="['gonitis:visit:edit']"
                >编辑阶段信息</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />

    <!-- 随访详情 -->
    <el-dialog
      title="随访详情"
      :visible.sync="detailVisible"
      width="50%"
      :before-close="detailClose"
    >
      <div class="visit-detail" v-loading="detailLoading">
        <div class="basic-info">
          <el-descriptions title="随访基础信息" direction="vertical" :column="3" border>
            <el-descriptions-item label="随访编号">{{
              visitInfo.id
            }}</el-descriptions-item>
            <el-descriptions-item label="患者编号">{{
              visitInfo.patientId
            }}</el-descriptions-item>
            <el-descriptions-item label="患者姓名">{{
              visitInfo.name
            }}</el-descriptions-item>
            <el-descriptions-item label="患者电话">{{
              visitInfo.phone
            }}</el-descriptions-item>
            <el-descriptions-item label="患者病情">{{
              visitInfo.subTitle
            }}</el-descriptions-item>
            <el-descriptions-item label="随访标题">{{
              visitInfo.visitTitle
            }}</el-descriptions-item>
            <el-descriptions-item label="随访轮次">{{
              "第" + visitInfo.visitCount + "轮"
            }}</el-descriptions-item>
            <el-descriptions-item label="随访状态">
              <dict-tag
                size="small"
                :options="dict.type.gonitis_visit_status"
                :value="visitInfo.visitStatus"
              />
            </el-descriptions-item>
            <el-descriptions-item label="创建时间">{{
              visitInfo.createTime
            }}</el-descriptions-item>
          </el-descriptions>
        </div>
        <div class="step-info">
          <h3 style="color: #00000; font-weight: bold">随访阶段信息</h3>
          <el-tabs v-model="activeName" type="border-card">
            <el-tab-pane
              :label="item.stepTitle"
              :key="index"
              :name="index + ''"
              v-for="(item, index) in stepList"
            >
              <el-collapse v-model="activeNames">
                <el-collapse-item
                  v-for="(moduleItem, index) in item.moduleListVo"
                  :key="index"
                  :title="moduleItem.moduleName"
                  :name="index + ''"
                >
                  <el-descriptions size="small">
                    <el-descriptions-item label="患者来源">{{
                      moduleItem.subName
                    }}</el-descriptions-item>
                    <el-descriptions-item label="模块备注">{{
                      moduleItem.remark
                    }}</el-descriptions-item>
                  </el-descriptions>
                  <el-card class="box-card">
                    <div
                      class="field-item"
                      :key="fieldItem.id"
                      v-for="fieldItem in moduleItem.params.fieldList"
                    >
                      <span style="margin-right: 10px">{{ fieldItem.fieldName }}</span>
                      <el-input
                        size="small"
                        style="width: 200px"
                        placeholder="请输入内容"
                        :disabled="true"
                        v-model="fieldItem.fieldValue"
                        v-if="fieldItem.fieldType === 0"
                      />
                      <el-input-number
                        v-model="fieldItem.fieldValue"
                        :disabled="true"
                        size="small"
                        style="width: 200px"
                        :min="1"
                        :max="10"
                        label="请输入内容"
                        v-else
                      />
                    </div>
                  </el-card>
                </el-collapse-item>
              </el-collapse>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailClose">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 弹窗, 新增 / 修改 -->
    <edit-step ref="visitDetail" @getDataList="getDataList"></edit-step>
    <edit v-if="editVisible" ref="edit" @getDataList="getDataList"></edit>
  </div>
</template>

<script>
import Edit from "./edit";
import EditStep from "./editStep";
import { listVisit, removeVisit, allVisit } from "@/api/gonitis/visit";
import { subTree } from "@/api/gonitis/illSub";
export default {
  name: "Visit",
  dicts: ["gonitis_visit_status"],
  data() {
    return {
      activeNames: [],
      stepInfo: {},
      activeName: "0",
      stepList: [],
      visitInfo: {},
      detailLoading: false,
      detailVisible: false,
      // 病情多选框映射
      defaultProps: {
        children: "children",
        label: "label",
        value: "id",
      },
      editVisible: false,
      // 显示搜索条件
      showSearch: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        visitStatus: undefined,
        visitTitle: undefined,
        patientId: undefined,
        subId: undefined,
      },
      // 时间间隔对象
      dateRange: [],
      // 部门下拉树对象
      deptOptions: [],
      dataList: [],
      total: 0,
      loading: false,
      ids: [],
    };
  },
  components: {
    Edit,
    EditStep,
  },
  created() {
    this.init();
  },
  methods: {
    /** 关闭详情 */
    detailClose() {
      this.visitInfo = {};
      this.stepList = [];
      this.detailVisible = false;
    },
    /** 跳转到模块管理 */
    toVisitForm() {
      this.$router.push({
        name: "VisitModule",
      });
    },
    /** 更多操作触发 */
    handleCommand(command, row) {
      switch (command) {
        case "detailHandle":
          this.detailHandle(row.id);
          break;
        case "editStepHandle":
          this.editStepHandle(row.id);
          break;
        default:
          break;
      }
    },
    /** 详情 */
    detailHandle(id) {
      this.detailLoading = true;
      this.detailVisible = true;
      allVisit(id)
        .then((response) => {
          const visitInfo = response.data.visitInfo;
          const stepList = response.data.stepList;
          this.visitInfo = visitInfo.params.patientInfo;
          this.visitInfo.visitTitle = visitInfo.visitTitle;
          this.visitInfo.visitCount = visitInfo.visitCount;
          this.visitInfo.visitStatus = visitInfo.visitStatus;
          this.visitInfo.patientId = visitInfo.patientId;
          this.visitInfo.id = visitInfo.id;
          this.visitInfo.createTime = visitInfo.createTime;
          this.stepList = stepList;
        })
        .catch(() => {})
        .finally(() => {
          this.detailLoading = false;
        });
    },
    /** 编辑随访阶段 */
    editStepHandle(id) {
      this.$nextTick(() => {
        this.$refs.visitDetail.init(id, 1);
      });
    },
    /** 完善随访 */
    completeVisit(id) {
      this.$nextTick(() => {
        this.$refs.visitDetail.init(id, 0);
      });
    },
    /** 初始化 */
    init() {
      this.getDataList();
      this.getDeptTree();
    },
    /** 病情分类选择框改变 */
    handleSubChange(value) {
      this.queryParams.subId = value[value.length - 1];
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      subTree().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    /** 表单重置 */

    resetQuery() {
      this.dateRange = [];
      this.queryParams.subId = undefined;
      this.queryParams.visitStatus = undefined;
      this.queryParams.patientId = undefined;
      this.queryParams.visitTitle = undefined;
      this.handleQuery();
    },
    /** 获取数据列表 */
    getDataList() {
      this.loading = true;
      listVisit(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.totalCount;
        this.loading = false;
      });
    },
    /** 多选 */
    selectionChangeHandle(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增 / 修改 */

    addOrUpdateHandle(id) {
      this.editVisible = true;
      this.$nextTick(() => {
        this.$refs.edit.init(id);
      });
    },
    /** 多选修改 */

    updateHandle() {
      let id = this.ids[0];
      this.editVisible = true;
      this.$nextTick(() => {
        this.$refs.edit.init(id);
      });
    },
    /** 删除 */

    deleteHandle(id) {
      const ids = id ? [id] : this.ids;
      this.$modal
        .confirm('是否确认删除【随访管理】索引为"' + ids + '"的数据项？')
        .then(() => {
          return removeVisit(ids);
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getDataList();
        });
    },
  },
};
</script>

<style scoped>
.field-item {
  margin-top: 10px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}
</style>
